from rest_framework import serializers
from .models import Department, Doctor, Hospital, Schedule, TimeSlot, MedicalArticle



class DepartmentSerializer(serializers.ModelSerializer):
    parent = serializers.PrimaryKeyRelatedField(
        queryset=Department.objects.all(),
        required=False,
        allow_null=True
    )

    class Meta:
        model = Department
        fields = [
            'id',
            'name',
            'parent',
            'campus',
            'address',
            'create_time',
            'full_path'  # 包含计算字段
        ]
        extra_kwargs = {
            'full_path': {'read_only': True},
            'create_time': {'read_only': True}
        }


class DoctorSerializer(serializers.ModelSerializer):
    """医生序列化器"""
    department_name = serializers.CharField(source='department.name', read_only=True)
    department_parent_name = serializers.CharField(source='department.parent.name', read_only=True, allow_null=True)
    full_department = serializers.CharField(read_only=True)  # 调用模型的full_department属性

    class Meta:
        model = Doctor
        fields = [
            'id',
            'name',
            'price',
            'avatar',
            'department',
            'department_name',      # 部门名称
            'department_parent_name',  # 父部门名称
            'full_department',      # 完整部门名称（如内科-心脏内科）
            'title',
            'tags',
            'introduction',
            'campus'
        ]
        read_only_fields = ['department_name', 'department_parent_name', 'full_department']


class HospitalSerializer(serializers.ModelSerializer):
    """医院信息序列化器"""
    class Meta:
        model = Hospital
        fields = ['id', 'name', 'address', 'campus']


class ScheduleSerializer(serializers.ModelSerializer):
    """医生排班序列化器"""
    doctor = DoctorSerializer(read_only=True)  # 嵌套医生详情
    doctor_id = serializers.PrimaryKeyRelatedField(
        queryset=Doctor.objects.all(), source='doctor', write_only=True, required=False
    )
    available_numbers = serializers.IntegerField(read_only=True)
    status_display = serializers.CharField(source='get_status_display', read_only=True)

    class Meta:
        model = Schedule
        fields = [
            'id',
            'doctor',         # 医生详情（嵌套对象，GET 时用）
            'doctor_id',      # 医生id（POST/PUT 时用）
            'date',
            'total_numbers',
            'booked_numbers',
            'available_numbers',
            'status',
            'status_display'
        ]
        read_only_fields = ['available_numbers', 'status_display']


class TimeSlotSerializer(serializers.ModelSerializer):
    """预约时间段序列化器"""
    class Meta:
        model = TimeSlot
        fields = [
            'id', 'schedule', 'start_time', 'end_time',
            'total_numbers', 'booked_numbers', 'available_numbers'
        ]


class MedicalArticleSerializer(serializers.ModelSerializer):
    """医疗文章序列化器"""
    department_name = serializers.CharField(source='department.name', read_only=True)
    tag_list = serializers.ListField(read_only=True)  # 获取标签列表
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    
    class Meta:
        model = MedicalArticle
        fields = [
            'id',
            'title',
            'content',
            'author',
            'department',
            'department_name',
            'tags',
            'tag_list',
            'image',
            'view_count',
            'like_count',
            'status',
            'status_display',
            'created_at',
            'updated_at'
        ]
        read_only_fields = ['view_count', 'like_count', 'created_at', 'updated_at', 'tag_list']





